//
//  ViewController.m
//  动画效果
//
//  Created by ios on 2016/11/16.
//  Copyright © 2016年 Wy. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic,strong)UITextField *mobileField;
@property(nonatomic,strong)UITextField *passwordField;
@property(nonatomic,strong)UILabel *loginLabel;
@property(nonatomic,strong)UIImageView *cloud1;
@property(nonatomic,strong)UIImageView *cloud2;
@property(nonatomic,strong)UIImageView *cloud3;
@property(nonatomic,strong)UIImageView *cloud4;
@property(nonatomic,strong)UIImageView *balloon1;
@property(nonatomic,strong)UIImageView *balloon2;
@property(nonatomic,strong)UIImageView *carImageView;

@property(nonatomic,assign)CGFloat x;
@property(nonatomic,assign)CGFloat y;

@end

@implementation ViewController


-(UIImageView *)balloon2{

    if (_balloon2 == nil) {
        self.y = arc4random()%568;
        self.x = arc4random()%320;
        _balloon2 = [[UIImageView alloc] initWithFrame:CGRectMake(self.x, self.y, 50, 65)];
        _balloon2.image = [UIImage imageNamed:@"balloon"];
        
        [self.view insertSubview:_balloon2 atIndex:3];
    }
    
    return _balloon2;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    self.view.userInteractionEnabled =YES;
    
    
    
    
    UIImageView *background = [[UIImageView alloc] initWithFrame:self.view.bounds];
    background.contentMode = UIViewContentModeScaleAspectFill;
    background.image = [UIImage imageNamed:@"gonglu.jpg"];
    background.userInteractionEnabled = YES;
    [self.view addSubview:background];
    
    self.mobileField = [[UITextField alloc] initWithFrame:CGRectMake(-200, 120, 200, 40)];
    self.mobileField.borderStyle = UITextBorderStyleRoundedRect;
    self.mobileField.placeholder =@"UserName";
    
    [self.view insertSubview:self.mobileField atIndex:8];
    
    self.passwordField = [[UITextField alloc] initWithFrame:CGRectMake(-200, 180, 200, 40)];
    self.passwordField.borderStyle = UITextBorderStyleRoundedRect;
    self.passwordField.placeholder = @"Password";
    [self.view addSubview:self.passwordField];
    
    
    self.loginLabel = [[UILabel alloc] initWithFrame:CGRectMake(-200, 80 , 200, 40)];
    self.loginLabel.text=@"登   录";
    self.loginLabel.textAlignment = NSTextAlignmentCenter;
    self.loginLabel.font = [UIFont boldSystemFontOfSize:20];
    self.loginLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:self.loginLabel];
    
    
    self.balloon1 = [[UIImageView alloc] initWithFrame:CGRectMake(20, 400, 50, 65)];
    self.balloon1.image = [UIImage imageNamed:@"balloon"];
    [self.view addSubview:self.balloon1];
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.x"];
    animation.fromValue =@(-200);
    animation.toValue =@(160);
    animation.duration = 0.5;
    animation.delegate =self;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    
    [self.loginLabel.layer addAnimation:animation forKey:@"frame"];
    
    animation.beginTime = CACurrentMediaTime()+0.2;
    
    [self.mobileField.layer addAnimation:animation forKey:@"frame"];
   
    animation.beginTime = CACurrentMediaTime()+0.5;
    [self.passwordField.layer addAnimation:animation forKey:@"frame"];
/* 垂直上升
    CABasicAnimation *animation_y = [CABasicAnimation animationWithKeyPath:@"position.y"];
    
    animation_y.fromValue =@(400);
    animation_y.toValue =@(80);
    animation_y.duration = 5;
    animation_y.removedOnCompletion = NO;
    animation_y.delegate = self;
    animation_y.fillMode = kCAFillModeForwards;
    [self.balloon1.layer addAnimation:animation_y forKey:nil];
    
    CABasicAnimation *animation_opacity = [CABasicAnimation animationWithKeyPath:@"opacity"];
    animation_opacity.fromValue =@(0);
    animation_opacity.toValue =@(1.0);
    animation_opacity.duration = 10;
    animation_opacity.removedOnCompletion = NO;
    animation_opacity.fillMode = kCAFillModeForwards;
    [self.balloon1.layer addAnimation:animation_opacity forKey:nil];
*/
    
    [self creatballoon];
    
    self.cloud1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 60, 64, 20)];
    self.cloud1.image = [UIImage imageNamed:@"bg-sunny-cloud-1"];
    [self.view insertSubview:self.cloud1 atIndex:4];
    
    
    self.cloud2 = [[UIImageView alloc] initWithFrame:CGRectMake(250, 80, 69, 24)];
    self.cloud2.image = [UIImage imageNamed:@"bg-sunny-cloud-2"];
    [self.view insertSubview:self.cloud2 atIndex:5];
    
    [self animationCloud];
    
    
    self.carImageView = [[UIImageView alloc] initWithFrame:CGRectMake(130, 380, 60, 60)];
    self.carImageView.image = [UIImage imageNamed:@"160.jpg"];
    
    [self.view addSubview:self.carImageView];
    
    CABasicAnimation *animation_scale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    animation_scale.fromValue =@(0.5);
    animation_scale.toValue =@(1.5);
    animation_scale.duration = 2.5;
    animation_scale.removedOnCompletion = NO;
    
    animation_scale.fillMode = kCAFillModeForwards;
    [self.carImageView.layer addAnimation:animation_scale forKey:nil];
}

-(void)animationDidStart:(CAAnimation *)anim{
    
    
}

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    //anim ==[self.balloon2.layer animationForKey:[NSString stringWithFormat:@"ballon+%f",self.y]]
    
    if (anim ==[self.loginLabel.layer animationForKey:@"frame"] ||  anim ==[self.mobileField.layer animationForKey:@"frame"] || anim ==[self.passwordField.layer animationForKey:@"frame"]) {
        self.loginLabel.frame = CGRectMake(40, 80, 200, 40);
        self.mobileField.frame = CGRectMake(40, 120, 200, 40);
        self.passwordField.frame = CGRectMake(40, 180, 200, 40);
        
    }else if (anim ==[self.balloon2.layer animationForKey:@"ballon"]) {
        [UIView animateWithDuration:0.5 animations:^{
          
        
        }completion:^(BOOL finished) {
            
            [self.balloon2 removeFromSuperview];
            self.balloon2 = nil;
            
            [self performSelector:@selector(creatballoon) withObject:self afterDelay:0.5];
        }];
    }else if (anim == [self.balloon2.layer animationForKey:@"opacity"]){
        CABasicAnimation *animation_opacity = [CABasicAnimation animationWithKeyPath:@"opacity"];
        animation_opacity.fromValue =@(1);
        animation_opacity.toValue =@(0);
        animation_opacity.duration = 2.5;
        animation_opacity.removedOnCompletion = NO;
        
        animation_opacity.fillMode = kCAFillModeForwards;
        [self.balloon2.layer addAnimation:animation_opacity forKey:nil];
    }else if (anim == [self.cloud1.layer animationForKey:@"coloud1"]){
        CABasicAnimation *right = [CABasicAnimation animationWithKeyPath:@"position.x"];
        right.fromValue = @(180);
        right.toValue =@(0);
        right.duration =8;
        right.removedOnCompletion =NO;
        right.delegate =self;
        right.fillMode =kCAFillModeForwards;
        [self.cloud1.layer addAnimation:right forKey:@"coloud_1"];
    }else if (anim == [self.cloud1.layer animationForKey:@"coloud_1"]){
        [self animationCloud];
    }else if (anim == [self.cloud2.layer animationForKey:@"coloud2"]){
        CABasicAnimation *right2 = [CABasicAnimation animationWithKeyPath:@"position.x"];
        right2.fromValue = @(320);
        right2.toValue =@(250);
        right2.duration =8;
        right2.removedOnCompletion =NO;
        right2.delegate =self;
        right2.fillMode =kCAFillModeForwards;
        [self.cloud2.layer addAnimation:right2 forKey:@"coloud_2"];
    }else if (anim == [self.cloud1.layer animationForKey:@"coloud_2"]){
        
    }
}

-(void)creatballoon{
    
    CABasicAnimation *animation_y = [CABasicAnimation animationWithKeyPath:@"position.y"];
    CGFloat y = arc4random()%568;
    
    animation_y.fromValue =@(self.y);
    animation_y.toValue =@(y);
    animation_y.duration = 5;
    animation_y.removedOnCompletion = NO;
    animation_y.delegate = self;
    animation_y.fillMode = kCAFillModeForwards;
   // [self.balloon2.layer addAnimation:animation_y forKey:[NSString stringWithFormat:@"ballon+%f",self.y]];
    animation_y.timingFunction = [CAMediaTimingFunction  functionWithName:kCAMediaTimingFunctionEaseInEaseOut] ;
    [self.balloon2.layer addAnimation:animation_y forKey:@"ballon"];
    
    CABasicAnimation *animation_x = [CABasicAnimation animationWithKeyPath:@"position.x"];
    CGFloat x = arc4random()%320;
    
    animation_x.fromValue =@(self.x);
    animation_x.toValue =@(x);
    animation_x.duration = 5;
    animation_x.removedOnCompletion = NO;
    animation_x.delegate = self;
    animation_x.fillMode = kCAFillModeForwards;
    animation_x.timingFunction = [CAMediaTimingFunction  functionWithName:kCAMediaTimingFunctionEaseInEaseOut] ;
    [self.balloon2.layer addAnimation:animation_x forKey:nil];
    
    CABasicAnimation *animation_opacity = [CABasicAnimation animationWithKeyPath:@"opacity"];
    animation_opacity.fromValue =@(0);
    animation_opacity.toValue =@(1);
    animation_opacity.duration = 2.5;
    animation_opacity.delegate = self;
    animation_opacity.removedOnCompletion = NO;
    animation_opacity.fillMode = kCAFillModeBackwards;
    [self.balloon2.layer addAnimation:animation_opacity forKey:@"opacity"];
    
}

-(void)animationCloud{
    CABasicAnimation *right1 = [CABasicAnimation animationWithKeyPath:@"position.x"];
    right1.fromValue = @(0);
    right1.toValue =@(180);
    right1.duration =8;
    right1.removedOnCompletion =NO;
    right1.delegate =self;
    right1.fillMode =kCAFillModeForwards;
    [self.cloud1.layer addAnimation:right1 forKey:@"coloud1"];
    
    
    CABasicAnimation *right2 = [CABasicAnimation animationWithKeyPath:@"position.x"];
    right2.fromValue = @(250);
    right2.toValue =@(320);
    right2.duration =8;
    right2.removedOnCompletion =NO;
    right2.delegate =self;
    right2.fillMode =kCAFillModeForwards;
    [self.cloud2.layer addAnimation:right2 forKey:@"coloud2"];
    
}












@end
